package controller

import (
	"fx_swift_boss/api"
	"fx_swift_boss/internal/service"
	"fx_swift_boss/pkg/code"
	"fx_swift_boss/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddPlacard(c *gin.Context) {
	req := &api.AddPlacardReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrBind, err.Error())
		return
	}

	err := service.NewPlacardSrv().AddPlacard(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

func GetPlacard(c *gin.Context) {
	req := &api.GetPlacardReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrBind, err.Error())
		return
	}
	list, err := service.NewPlacardSrv().GetPlacard(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, list)
	}
}

func DelPlacard(c *gin.Context) {
	id := c.Query("id")
	err := service.NewPlacardSrv().DelPlacard(c, id)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}
